<template>
  <div class="app-container">

    <div style="padding: 5px;" />
    <el-button type="primary" size="small" @click="onclickAdd">添加请假类别</el-button>

    <div style="padding: 5px;" />
    <el-table :data="list" border row-key="id" align="left">
      <el-table-column label="编号" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column label="请假类别" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">

          <el-button type="danger" size="mini" plain @click="onClickDel(scope.row.id)"> 删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新增请假类别" width="30%" :visible.sync="addDialog.visible">
      <el-form>
        <el-form-item label="请假类别">
          <el-input v-model="addDialog.Name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="add">新 建</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { websock } from '@/websock'
import { MessageBox } from 'element-ui'

export default {
  data() {
    return {
      list: null,
      addDialog: {
        visible: false,
        Name: ''
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    // 获取数据
    fetchData() {
      websock.qingjiaList().then(data => {
        this.list = data
      })
    },
    // 点击新增
    onclickAdd() {
      this.addDialog.visible = true
      this.addDialog.Name = ''
    },
    // 点击删除
    onClickDel(id) {
      MessageBox.confirm('是否删除', '是否删除', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        websock.deleteQingjia({ id }).then(() => {
          this.fetchData()
        })
      })
    },
    // 点击增加
    add() {
      this.addDialog.visible = false
      websock.addQingjia({
        name: this.addDialog.Name
      }).then(() => {
        this.fetchData()
      })
    }
  }
}
</script>
